#ifndef MEMPAGE_T_H_
#define MEMPAGE_T_H_
#include <string.h>

typedef unsigned char byte;

class memPage_t {
public:
	memPage_t(); //Default constructor
	memPage_t(int pageSize);

	virtual ~memPage_t(); // Destructor

	int getCurrentPos() const {
		return m_curPos;
	}

	bool isEmpty() const {
		return (m_actualSize == 0);
	}

	bool isFull() const {
		return (m_actualSize == m_capacity);
	}

	int getActualSize() const {
		return m_actualSize;
	}

	int getCapacity() const {
		return m_capacity;
	}

	bool setCurrentPos(int newPos);

	template<class T> bool write(const T& obj, int amount) {
		return write(obj, amount, m_curPos);
	}

	template<class T> bool write(const T& obj, int amount, int pos) {
		if ((amount < 0) || (pos < 0) || (pos + amount > m_capacity)) {
			return false;
		}
		if (!setCurrentPos(pos)) {
			return false;
		}

		memcpy(m_buf + m_curPos, &obj, amount);
		m_curPos += amount;

		m_actualSize = (m_curPos > m_actualSize) ? m_curPos : m_actualSize;

		return true;
	}

	template<class T> bool read(T& obj, int amount) {
		return read(obj, amount, m_curPos);
	}

	template<class T> bool read(T& obj, int amount, int pos) {
		if ((amount < 0) || (pos < 0) || (pos + amount > m_actualSize)) {
			return false;
		}
		if (!setCurrentPos(pos)) {
			return false;
		}

		memcpy(&obj, m_buf + m_curPos, amount);
		m_curPos += amount;

		return true;
	}

	static void setDefaultPageSize(int pageSize) {
		if (pageSize > 0) {
			defaultPageSize = pageSize;
		}
	}

	static int getDefaultPageSize() {
		return defaultPageSize;
	}

private:
	int m_actualSize;
	const int m_capacity;
	int m_curPos;

	byte* m_buf;

	static int defaultPageSize;

	// Don't allow copy of object
	memPage_t(const memPage_t& mp); // Copy constructor
	const memPage_t& operator=(const memPage_t& mp); //Assignment operator

	void constructPage();
};
#endif /*MEMPAGE_T_H_*/
